
<!DOCTYPE HTML>
<html lang="" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>2.3 导入与配置 · GitBook</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.2">
        
        
        
    
    <link rel="stylesheet" href="../gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-search/search.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="3模型关系.html" />
    
    
    <link rel="prev" href="5.模型字段类型.html" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="Type to search" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="../">
            
                <a href="../">
            
                    
                    Introduction
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="1.写在前面的话.html">
            
                <a href="1.写在前面的话.html">
            
                    
                    写在前面的话
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="2.搭建环境.html">
            
                <a href="2.搭建环境.html">
            
                    
                    第一章 搭建环境
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4" >
            
                <span>
            
                    
                    第二章 模型定义
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.4.1" data-path="3.sequelize 实例变量.html">
            
                <a href="3.sequelize 实例变量.html">
            
                    
                    2.1 实例变量方法
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.2" data-path="4.定义一个模型.html">
            
                <a href="4.定义一个模型.html">
            
                    
                    2.2 定义一个模型
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.3" data-path="5.模型字段类型.html">
            
                <a href="5.模型字段类型.html">
            
                    
                    2.3 模型字段类型
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="1.4.4" data-path="6.导入与配置.html">
            
                <a href="6.导入与配置.html">
            
                    
                    2.3 导入与配置
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.5" data-path="3模型关系.html">
            
                <a href="3模型关系.html">
            
                    
                    第三章 模型关系
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.5.1" data-path="3.1属于.html">
            
                <a href="3.1属于.html">
            
                    
                    3.1 属于
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.2" data-path="3.2包含.html">
            
                <a href="3.2包含.html">
            
                    
                    3.2 包含
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.3" data-path="3.3一对多.html">
            
                <a href="3.3一对多.html">
            
                    
                    3.3 一对多
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.4" data-path="3.4多对多.html">
            
                <a href="3.4多对多.html">
            
                    
                    3.4 多对多
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.5" data-path="3.5多态一对多.html">
            
                <a href="3.5多态一对多.html">
            
                    
                    3.5 多态一对多
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.6" data-path="3.6多态多对多.html">
            
                <a href="3.6多态多对多.html">
            
                    
                    3.6 多态多对多
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.7" data-path="3.7关系嵌套.html">
            
                <a href="3.7关系嵌套.html">
            
                    
                    3.7 关系嵌套
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.6" data-path="4模型查询.html">
            
                <a href="4模型查询.html">
            
                    
                    第四章 模型查询
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7" data-path="5查询范围.html">
            
                <a href="5查询范围.html">
            
                    
                    第五章 查询范围 Scope (预定义查询条件)
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8" data-path="6生命周期函数.html">
            
                <a href="6生命周期函数.html">
            
                    
                    第六章 生命周期函数 Hook
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.9" data-path="7migrate.html">
            
                <a href="7migrate.html">
            
                    
                    第七章 数据库迁移 Migrate
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.10" data-path="8tabel2model.html">
            
                <a href="8tabel2model.html">
            
                    
                    第八章 根据 Table 生成 Model
            
                </a>
            

            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            Published with GitBook
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href=".." >2.3 导入与配置</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <h1 id="&#x6A21;&#x578B;&#x914D;&#x7F6E;&#x9879;">&#x6A21;&#x578B;&#x914D;&#x7F6E;&#x9879;</h1>
<p>&#x5148;&#x4E86;&#x89E3;&#x4E00;&#x4E0B;&#x914D;&#x7F6E;&#x9879;&#xFF0C;&#x5173;&#x4E8E; Scope &#x7684;&#xFF0C;&#x5728;&#x540E;&#x7EED;&#x7684;&#x7AE0;&#x8282;&#x6162;&#x6162;&#x8865;&#x5145;&#x3002;define &#x7684;&#x7B2C;&#x4E09;&#x4E2A;&#x53C2;&#x6570;&#x5C31;&#x662F;&#x6A21;&#x578B;&#x7684;&#x914D;&#x7F6E;&#x9879;&#xFF0C;&#x5B83;&#x7684;&#x7C7B;&#x578B;&#x662F;<code>DefineOptions</code>&#x63A5;&#x53E3;&#xFF0C;&#x63A5;&#x53E3;&#x5982;&#x4E0B;&#x3002;</p>
<pre><code class="lang-ts">defaultScope?: FindOptions;
scopes?: DefineScopeOptions; <span class="hljs-comment">// scopes &#x5728;&#x540E;&#x7EED;&#x7684;&#x4F1A;&#x6709;&#x4E00;&#x4E2A;&#x5927;&#x7684;&#x7AE0;&#x8282;&#x6765;&#x8BF4;</span>
omitNull?: <span class="hljs-built_in">boolean</span>; <span class="hljs-comment">// &#x6240;&#x6709;&#x7684;&#x5B57;&#x6BB5;&#x90FD;&#x4E0D;&#x80FD;&#x4E3A;&#x7A7A;</span>
timestamps?: <span class="hljs-built_in">boolean</span>; <span class="hljs-comment">// &#x5173;&#x95ED;&#x65F6;&#x95F4;&#x6233;&#x5B57;&#x6BB5;</span>
paranoid?: <span class="hljs-built_in">boolean</span>; <span class="hljs-comment">// &#x8F6F;&#x5220;&#x9664;</span>
underscored?: <span class="hljs-built_in">boolean</span>; <span class="hljs-comment">// &#x6570;&#x636E;&#x5E93;&#x8868;&#x5B57;&#x6BB5;&#x5355;&#x8BCD;&#x5206;&#x5272;&#x6539;&#x6210;&#x4E0B;&#x5212;&#x7EBF; createdAt -&gt; created_at</span>
underscoredAll?: <span class="hljs-built_in">boolean</span>; <span class="hljs-comment">// &#x6570;&#x636E;&#x5E93;&#x6A21;&#x578B;&#x5355;&#x8BCD;&#x6539;&#x6210;&#x4E0B;&#x5212;&#x7EBF;</span>
hasTrigger?: <span class="hljs-built_in">boolean</span>; <span class="hljs-comment">// &#x662F;&#x5426;&#x6709;&#x76F8;&#x5173;&#x7684;&#x89E6;&#x53D1;&#x5668;</span>
freezeTableName?: <span class="hljs-built_in">boolean</span>; <span class="hljs-comment">// &#x8BA9;&#x6570;&#x636E;&#x5E93;&#x8868;&#x4E0E;&#x6A21;&#x578B;&#x4FDD;&#x6301;&#x4E00;&#x81F4;&#xFF0C;&#x901A;&#x5E38;&#x6570;&#x636E;&#x5E93;&#x8868;&#x9ED8;&#x8BA4;&#x662F;&#x82F1;&#x6587;&#x590D;&#x6570;</span>
name?: DefineNameOptions; <span class="hljs-comment">// &#x5BF9;&#x5173;&#x8054;&#x6A21;&#x578B;&#x7684;&#x4E00;&#x4E9B;&#x914D;&#x7F6E;</span>
indexes?: DefineIndexesOptions[]; <span class="hljs-comment">// &#x6DFB;&#x52A0;&#x7D22;&#x5F15;</span>
createdAt?: <span class="hljs-built_in">string</span> | <span class="hljs-built_in">boolean</span>; <span class="hljs-comment">// &#x521B;&#x5EFA;&#x65F6;&#x95F4;&#x6233;</span>
deletedAt?: <span class="hljs-built_in">string</span> | <span class="hljs-built_in">boolean</span>; <span class="hljs-comment">// &#x5220;&#x9664;&#x65F6;&#x95F4;&#x6233;</span>
updatedAt?: <span class="hljs-built_in">string</span> | <span class="hljs-built_in">boolean</span>; <span class="hljs-comment">// &#x66F4;&#x65B0;&#x65F6;&#x95F4;&#x6233;</span>
tableName?: <span class="hljs-built_in">string</span>; <span class="hljs-comment">// &#x8BBE;&#x7F6E;&#x8868;&#x540D;</span>
getterMethods?: DefineGetterMethodsOptions; <span class="hljs-comment">// &#x8BBE;&#x7F6E;&#x5BF9;&#x8C61;&#x7684; getter &#x65B9;&#x6CD5;</span>
setterMethods?: DefineSetterMethodsOptions;
instanceMethods?: <span class="hljs-built_in">Object</span>; <span class="hljs-comment">// &#x5B9E;&#x4F8B;&#x65B9;&#x6CD5;</span>
classMethods?: <span class="hljs-built_in">Object</span>; <span class="hljs-comment">// &#x9759;&#x6001;&#x65B9;&#x6CD5;</span>
engine?: <span class="hljs-built_in">string</span>; <span class="hljs-comment">// &#x8BBE;&#x7F6E;&#x8868;&#x5F15;&#x64CE;</span>
charset?: <span class="hljs-built_in">string</span>; <span class="hljs-comment">// &#x5B57;&#x7B26;&#x96C6;</span>
comment?: <span class="hljs-built_in">string</span>; <span class="hljs-comment">// &#x8868;&#x6CE8;&#x91CA;</span>
collate?: <span class="hljs-built_in">string</span>; <span class="hljs-comment">// &#x6821;&#x5BF9;&#x96C6;&#xFF0C;&#x8DDF;&#x6392;&#x5E8F;&#x76F8;&#x5173;&#x7684;&#x4E1C;&#x897F;</span>
initialAutoIncrement?: <span class="hljs-built_in">string</span>; <span class="hljs-comment">// &#x81EA;&#x589E;&#x7684;&#x521D;&#x59CB;&#x503C;</span>
hooks?: HooksDefineOptions&lt;TInstance&gt;; <span class="hljs-comment">// &#x6302;&#x8F7D;&#x6A21;&#x578B;&#x7684;&#x751F;&#x547D;&#x5468;&#x671F;&#x51FD;&#x6570;</span>
validate?: DefineValidateOptions; <span class="hljs-comment">// &#x9A8C;&#x8BC1;&#x65B9;&#x6CD5;</span>
</code></pre>
<p><strong>&#x8F6F;&#x5220;&#x9664;</strong>&#xFF1A;&#x4E0D;&#x4F1A;&#x771F;&#x6B63;&#x7684;&#x4ECE;&#x6570;&#x636E;&#x5E93;&#x4E2D;&#x5220;&#x9664;&#x67D0;&#x4E00;&#x884C;&#x6570;&#x636E;&#xFF0C;&#x53EA;&#x662F;&#x7528;&#x4E00;&#x4E2A; deletedAt &#x5B57;&#x6BB5;&#x6765;&#x6807;&#x8BC6;&#x5DF2;&#x7ECF;&#x5220;&#x9664;&#x3002;</p>
<h3 id="&#x901A;&#x8FC7;&#x6587;&#x4EF6;&#x5BFC;&#x5165;&#x6A21;&#x578B;">&#x901A;&#x8FC7;&#x6587;&#x4EF6;&#x5BFC;&#x5165;&#x6A21;&#x578B;</h3>
<p>&#x901A;&#x8FC7;&#x6587;&#x4EF6;&#x5BFC;&#x5165;&#x6A21;&#x578B;&#xFF0C;&#x4F1A;&#x7528;&#x5230;&#x5B9E;&#x4F8B;&#x65B9;&#x6CD5;<code>import</code>&#xFF0C;&#x9996;&#x5148;&#x65B0;&#x5EFA;&#x4E00;&#x4E2A;<code>phone.ts</code>&#x6587;&#x4EF6;&#x3002;</p>
<ol>
<li>phone.ts</li>
</ol>
<pre><code class="lang-ts"><span class="hljs-keyword">import</span> Sequelize from <span class="hljs-string">&apos;sequelize&apos;</span>;

<span class="hljs-keyword">const</span> S = Sequelize;

<span class="hljs-keyword">export</span> <span class="hljs-keyword">interface</span> PhoneAttributes {
  <span class="hljs-keyword">type</span>: <span class="hljs-built_in">string</span>;
  name: <span class="hljs-built_in">string</span>;
}

<span class="hljs-keyword">export</span> <span class="hljs-keyword">interface</span> PhoneInstance <span class="hljs-keyword">extends</span> Sequelize.Instance&lt;PhoneAttributes&gt; {
  id: <span class="hljs-built_in">number</span>;
  createdAt: <span class="hljs-built_in">Date</span>;
  updatedAt: <span class="hljs-built_in">Date</span>;

  <span class="hljs-keyword">type</span>: <span class="hljs-built_in">string</span>;
  name: <span class="hljs-built_in">string</span>;
}

<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">PhoneDefine</span>(<span class="hljs-params">sequelize: Sequelize.Sequelize, dataTypes: Sequelize.DataTypes</span>) </span>{
  <span class="hljs-keyword">return</span> sequelize.define&lt;PhoneInstance, PhoneAttributes&gt;(<span class="hljs-string">&apos;Phone&apos;</span>, {
    <span class="hljs-keyword">type</span>: dataTypes.STRING,
    name: dataTypes.STRING
  })
}
</code></pre>
<ol>
<li>index.ts</li>
</ol>
<pre><code class="lang-ts"><span class="hljs-keyword">import</span> PhoneDefine , { PhoneAttributes, PhoneInstance }  from <span class="hljs-string">&apos;./phone&apos;</span>;

async <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">main</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-keyword">try</span> {
    <span class="hljs-comment">// const Phone = sequelize.import&lt;PhoneInstance, PhoneAttributes&gt;(&apos;Phone&apos;, PhoneDefine);</span>
    <span class="hljs-keyword">const</span> Phone = sequelize.import&lt;PhoneInstance, PhoneAttributes&gt;(<span class="hljs-string">&apos;./phone&apos;</span>);

    await Phone.sync()
    <span class="hljs-keyword">let</span> phone = await Phone.create({name:<span class="hljs-string">&quot;IPhone&quot;</span>, <span class="hljs-keyword">type</span>: <span class="hljs-string">&quot;Apple&quot;</span>})
    <span class="hljs-built_in">console</span>.log(phone);

  }<span class="hljs-keyword">catch</span>(e){
    <span class="hljs-comment">// console.log(e)</span>
    <span class="hljs-keyword">if</span> (e <span class="hljs-keyword">instanceof</span> Sequelize.ValidationError) {
      <span class="hljs-built_in">console</span>.log(e.message);
    }
  }
  process.exit(<span class="hljs-number">0</span>)
}
</code></pre>
<p>import &#x53EF;&#x4EE5;&#x4F20;&#x5165;&#x8DEF;&#x5F84;&#xFF0C;&#x4E5F;&#x53EF;&#x4EE5;&#x4F20;&#x5B9A;&#x4E49;&#x51FD;&#x6570;&#x3002;</p>

                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="5.模型字段类型.html" class="navigation navigation-prev " aria-label="Previous page: 2.3 模型字段类型">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="3模型关系.html" class="navigation navigation-next " aria-label="Next page: 第三章 模型关系">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"2.3 导入与配置","level":"1.4.4","depth":2,"next":{"title":"第三章 模型关系","level":"1.5","depth":1,"path":"document/3模型关系.md","ref":"./document/3模型关系.md","articles":[{"title":"3.1 属于","level":"1.5.1","depth":2,"path":"document/3.1属于.md","ref":"./document/3.1属于.md","articles":[]},{"title":"3.2 包含","level":"1.5.2","depth":2,"path":"document/3.2包含.md","ref":"./document/3.2包含.md","articles":[]},{"title":"3.3 一对多","level":"1.5.3","depth":2,"path":"document/3.3一对多.md","ref":"./document/3.3一对多.md","articles":[]},{"title":"3.4 多对多","level":"1.5.4","depth":2,"path":"document/3.4多对多.md","ref":"./document/3.4多对多.md","articles":[]},{"title":"3.5 多态一对多","level":"1.5.5","depth":2,"path":"document/3.5多态一对多.md","ref":"./document/3.5多态一对多.md","articles":[]},{"title":"3.6 多态多对多","level":"1.5.6","depth":2,"path":"document/3.6多态多对多.md","ref":"./document/3.6多态多对多.md","articles":[]},{"title":"3.7 关系嵌套","level":"1.5.7","depth":2,"path":"document/3.7关系嵌套.md","ref":"./document/3.7关系嵌套.md","articles":[]}]},"previous":{"title":"2.3 模型字段类型","level":"1.4.3","depth":2,"path":"document/5.模型字段类型.md","ref":"./document/5.模型字段类型.md","articles":[]},"dir":"ltr"},"config":{"gitbook":"*","theme":"default","variables":{},"plugins":["livereload"],"pluginsConfig":{"livereload":{},"highlight":{},"search":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"}},"file":{"path":"document/6.导入与配置.md","mtime":"2017-06-19T08:57:17.000Z","type":"markdown"},"gitbook":{"version":"3.2.2","time":"2017-06-28T08:29:37.466Z"},"basePath":"..","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="../gitbook/gitbook.js"></script>
    <script src="../gitbook/theme.js"></script>
    
        
        <script src="../gitbook/gitbook-plugin-livereload/plugin.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-search/search-engine.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-search/search.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-sharing/buttons.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    </body>
</html>

